package reduce.fz.com.reduce.fragment;


import android.app.Fragment;
import android.app.usage.UsageEvents;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.activity.TeamWitnessActivity;
import reduce.fz.com.reduce.adapter.BaseListAdapter;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.MyTeam;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.result.UserWitness;
import reduce.fz.com.reduce.bean.style.TextSpan;
import reduce.fz.com.reduce.event.BaseEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.TextSpanUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyTeamFragment extends BaseListFragment<MyTeamFragment.MyViewHolder, MyTeam> implements View.OnClickListener {
    private String requesturl;
    private Long uid;

    public static MyTeamFragment create(Long uid) {
        Bundle bundle = new Bundle();
        if (uid != null)
            bundle.putLong("uid", uid);
        MyTeamFragment fragment = new MyTeamFragment();
        fragment.setArguments(bundle);
        return fragment;

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        uid = getArguments().getLong("uid");
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public MyTeamFragment() {
    }

    @Override
    public MyViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.myteam_card_view, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public MyViewHolder createfootViewHolder(View view) {
        return new MyViewHolder(view);
    }


    @Override
    public String createReqUrl() {
        return requesturl;
    }

    @Override
    public void onClick(View v) {

    }


    public static class MyViewHolder extends BaseRecycleViewHolder<MyTeam> implements View.OnClickListener {
        private ImageView iv_team_image;
        private TextView tv_team_title;
        private MyTeamInterface myTeamInterface;
        private TextView mTvMeKepping;
        private TextView mTvKeeping;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_team_image = (ImageView) itemView.findViewById(R.id.iv_team_image);
            tv_team_title = (TextView) itemView.findViewById(R.id.tv_team_title);
            mTvMeKepping = (TextView) itemView.findViewById(R.id.tv_me_kepping);
            mTvKeeping = (TextView) itemView.findViewById(R.id.tv_kepping);

        }

        public MyViewHolder(View itemView, MyTeamInterface myTeamInterface) {
            this(itemView);
            this.myTeamInterface = myTeamInterface;
        }

        public void setMeKeppingStyle(TextView textView,Long days) {
            String text="您已坚持{0}天";
            TextSpan span=new TextSpan(days+"",R.style.my_team_blue_big_text_style);
            TextSpanUtil.set(context,textView,text,R.style.my_team_white_small_text_style,span);
        }
        public void setKeppingStyle(TextView textView,Long pepolecount)
        {
            String text="{0}人参与见证中";
            TextSpan span=new TextSpan(pepolecount+"",R.style.my_team_blue_peoplecount_text_style);
            TextSpanUtil.set(context,textView,text,R.style.my_team_white_small_text_style,span);
        }




        @Override
        public void renderView(MyTeam data,int position) {
            //Picasso.with(context).load(data.user.getHeadurl());
            Picasso.with(context).load(R.drawable.test_myteam).into(iv_team_image);
            iv_team_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new BaseEvent("forwardMyWitness"));
                }
            });
            setMeKeppingStyle(mTvMeKepping,35L);
            setKeppingStyle(mTvKeeping,223L);

            //tv_user_nickname.setText(data.user.getNickname());
            //tv_pub_time.setText(data.pubTime + "");
            //Picasso.with(context).load(data.user.getHeadurl());
        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_team_image:
                    break;
                default:
                    break;
            }
        }
    }

    public void request(String url) {
        List<MyTeam> list = new ArrayList<MyTeam>();
        for (int i = 0; i < 6; i++) {
            list.add(new MyTeam());
        }
        baseListAdapter.addDatas(list);


    }

    public interface MyTeamInterface {

        public void onMyTeam();
    }


    public void forwardWitness() {
        Intent intent = new Intent(this.getActivity(), TeamWitnessActivity.class);
        startActivity(intent);
    }

    public void onEvent(BaseEvent event) {
        if ("forwardMyWitness".equals(event.getCode())) {
            this.forwardWitness();
        }

    }


}
